package com.rd.student.jiaji.class2.shirui.arrtest;

import java.util.Arrays;

public class TestArr {
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
        TestArr t = new TestArr();
//        t.test1(arr);
        int[] arr1 = {1, 3, 5, 7, 9, 11, 13, 15, 17};
//        t.test2(arr1);

        t.test3(arr);
        t.test4(arr1);
    }

    public void test1(int[] arr) {

        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 9) {
                index = i;   //  4
                break;
            }
        }
        // 向后移动元素
        //  1, 3, 5, 7, 9, 11, 13, 15, 17, 0
        //  17->0  15->17  13->15 11->13
        for (int i = arr.length - 2; i > index; i--) {
            arr[i + 1] = arr[i];
        }

        arr[index + 1] = 10;

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public void test2(int[] arr) {
        int index = -1;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 11) {
                index = i; //5
                break;
            }
        }
        //向前移动元素
        //1, 3, 5, 7, 9, 11, 13, 15, 17
        // 13->11 15->13 17->15
        for (int j = index; j < arr.length - 1; j++) {
            arr[j] = arr[j + 1];
        }
        arr[arr.length - 1] = 0;

        for (int k = 0; k < arr.length; k++) {
            System.out.print(arr[k] + " ");
        }
        System.out.println();
    }

    public void test3(int[] arr) {
        //  int[] arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
        int[] arrC = new int[10];
        System.arraycopy(arr, 0, arrC, 0, 5);
        arrC[5] = 10;
        // {1, 3, 5, 7, 9, 10, 13, 15, 17, 0};
        System.arraycopy(arr, 6, arrC, 6, 4);
        System.out.println(Arrays.toString(arrC));
    }

    public void test4(int[] arr) {
        // int[] arr1 = {1, 3, 5, 7, 9, 11, 13, 15, 17};
        arr[5] = 0;
        int[] arrC = new int[9];
        System.arraycopy(arr, 0, arrC, 0, 5);
        // 1, 3, 5, 7, 9,0,0,0,0,0
        System.arraycopy(arr, 6, arrC, 5, 3);
        System.out.println(Arrays.toString(arrC));
    }

}
